package com.adcc.foc.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import java.nio.charset.StandardCharsets;

public class CryptoUtil {

    private static final String KEY = "adcc";

    /**
     * 加密
     *
     * @return
     */
    public static String encryptString(String data, String key) {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, (key + KEY).getBytes(StandardCharsets.UTF_8));
        return aes.encryptBase64(data, StandardCharsets.UTF_8);
    }

    /**
     * 解密
     *
     * @param data
     * @return
     */
    public static String decryptString(String data, String key) {
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, (key + KEY).getBytes(StandardCharsets.UTF_8));
        return new String(aes.decrypt(data), StandardCharsets.UTF_8);
    }

    /**
     * 随机生成密钥
     *
     * @return
     */
    public static String genAesSecret() {
        String secret = RandomUtil.randomString(12);
        return secret;
    }
}
